/*********************************************************
 *********************************************************
 ********************                  *******************
 *************                                ************
 *******                  _oo0oo_                  *******
 ***                     o8888888o                     ***
 *                       88" . "88                       *
 *                       (| -_- |)                       *
 *                       0\  =  /0                       *
 *                     ___/`---'\___                     *
 *                   .' \\|     |// '.                   *
 *                  / \\|||  :  |||// \                  *
 *                 / _||||| -:- |||||- \                 *
 *                |   | \\\  -  /// |   |                *
 *                | \_|  ''\---/''  |_/ |                *
 *                \  .-\__  '-'  ___/-. /                *
 *              ___'. .'  /--.--\  `. .'___              *
 *           ."" '<  `.___\_<|>_/___.' >' "".            *
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |          *
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /          *
 *      =====`-.____`.___ \_____/___.-`___.-'=====       *
 *                        `=---='                        *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      *
 *********__佛祖保佑__永无BUG__验收通过__钞票多多__*********
 *********************************************************/
package org.jbase.jbsc.commons.base.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionLikeType;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Project: jbsc-commons <br/>
 * File: JsonUtilJackson.java <br/>
 * Class: org.jbase.jbsc.commons.base.utils.JsonUtilJackson <br/>
 * Description: <描述类的功能>. <br/>
 * Copyright: Copyright (c) 2011 <br/>
 * Company: https://gitee.com/liuzp315 <br/>
 * Makedate: 2020/8/26 下午6:50 <br/>
 *
 * @author popo
 * @version 1.0
 * @since 1.0
 */
public abstract class JsonUtilJackson {

    private static ObjectMapper MAPPER = new ObjectMapper();

    public static Map<String, Object> str2map(String jsonStr) {
        // JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Object.class);
        // 如果是Map类型 mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);
        JavaType javaType = MAPPER.getTypeFactory().constructParametricType(HashMap.class, String.class, Object.class);
        Map<String, Object> map = null;
        try {
            map = MAPPER.readValue(jsonStr, javaType);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return map;
    }

    public static List<Map<String, Object>> str2list(String jsonStr) {
        CollectionLikeType type = MAPPER.getTypeFactory().constructCollectionLikeType(List.class, Map.class);
        List<Map<String, Object>> listMap = null;
        try {
            listMap = MAPPER.readValue(jsonStr, type);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // ObjectMapper mapper = new ObjectMapper();
        // JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Object.class);
        // //如果是Map类型 mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);
        // List<Bean> lst = (List<Bean>)mapper.readValue(jsonString, javaType);
        return listMap;
    }

    public static String obj2str(Object obj) {
        String mjson = null;
        try {
            MAPPER.setSerializationInclusion(JsonInclude.Include.ALWAYS);
            mjson = MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mjson;
    }

    public static String obj2str(Object obj, JsonInclude.Include inc) {
        String mjson = null;
        try {
            MAPPER.setSerializationInclusion(inc);
            mjson = MAPPER.writeValueAsString(obj);
        } catch (JsonProcessingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return mjson;
    }
}
